<?php
class Category_Model extends CI_Model {
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;		
	}
	public function get($intCategoryId) {		
		$stmt = $this->database->prepare('call categories_get(?)');		
		$stmt->bindParam(1, $intCategoryId, PDO::PARAM_INT, 11);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();			
		return isset($arrResult) ? $arrResult : null;		
	}	
	public function getAll($intAdType, $intType, $intStatus, $strStart, $strEnd, $intOff, $intLimit, &$intTotal=0) {							
		$stmt = $this->database->prepare('call categories_getall_a(?,?,?,?,?,
                                                                    ?,?)');					
		$stmt->bindParam(1, $intAdType    , PDO::PARAM_INT, 4);
		$stmt->bindParam(2, $intType      , PDO::PARAM_INT, 4);	
		$stmt->bindParam(3, $intStatus    , PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $strStart     , PDO::PARAM_STR, 10);
		$stmt->bindParam(5, $strEnd       , PDO::PARAM_STR, 10);
		$stmt->bindParam(6, $intOff       , PDO::PARAM_INT, 11);
		$stmt->bindParam(7, $intLimit     , PDO::PARAM_INT, 4);
        $stmt->execute();
		$arrResult 	= $stmt->fetchAll();
		$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
		return isset($arrResult) ? $arrResult : null;
	
	}
	
	public function getCategory($intType, $intStore=1) {			
		$stmt = $this->database->prepare('call categories_getall_type(?,?)');		
		$stmt->bindParam(1, $intType , PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intStore, PDO::PARAM_INT, 11);
        $stmt->execute();
		$arrResult 	= $stmt->fetchAll();			
		return isset($arrResult) ? $arrResult : null;		
	}
	public function getParent($intType) {	
		$stmt = $this->database->prepare('call categories_getall_parent(?)');		
		$stmt->bindParam(1, $intType, PDO::PARAM_INT, 11);
        $stmt->execute();
		$arrResult 	= $stmt->fetchAll();			
		return isset($arrResult) ? $arrResult : null;		
	}
	public function getChild($intParentId) {			
		$stmt = $this->database->prepare('call categories_getall_child(?)');		
		$stmt->bindParam(1, $intParentId, PDO::PARAM_INT, 11);
        $stmt->execute();
		$arrResult 	= $stmt->fetchAll();			
		return isset($arrResult) ? $arrResult : null;		
	}
	/**/
	public function add($intLoginId, $strName, $strSlug, $strShort, $strIcon, 
						$strIconAct, $intParentId, $intIseq, $intType=0, $strTag=''						
						) {
		$stmt = $this->database->prepare('call categories_add(?,?,?,?,?,
											?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId   , PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $strName      , PDO::PARAM_STR, 150);			
		$stmt->bindParam(3, $strSlug      , PDO::PARAM_STR, 255);			
		$stmt->bindParam(4, $strShort     , PDO::PARAM_STR, 255);			
		$stmt->bindParam(5, $strIcon      , PDO::PARAM_STR, 150);
		$stmt->bindParam(6, $strIconAct   , PDO::PARAM_STR, 150);			
		$stmt->bindParam(7, $intParentId  , PDO::PARAM_INT, 11);
		$stmt->bindParam(8,	$intIseq      , PDO::PARAM_INT, 11);
		$stmt->bindParam(9, $intType      , PDO::PARAM_INT, 4);
		$stmt->bindParam(10, $strTag      , PDO::PARAM_STR, 100);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;
		if($intError == 0) {	
		}
		return $intError;
	}		
	public function update($intLoginId, $intCategoryId, $strName, $strSlug, $strShort,
                            $strIcon, $strIconAct, $intIseq, $intType=0, $strTag=''
						   ) {
		
		$stmt = $this->database->prepare('call categories_update(?,?,?,?,?,
											   ?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId       , PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intCategoryId    , PDO::PARAM_INT, 11);		
		$stmt->bindParam(3, $strName          , PDO::PARAM_STR, 150);						
		$stmt->bindParam(4, $strSlug          , PDO::PARAM_STR, 255);			
		$stmt->bindParam(5, $strShort         , PDO::PARAM_STR, 255);			
		$stmt->bindParam(6, $strIcon          , PDO::PARAM_STR, 150);
		$stmt->bindParam(7, $strIconAct       , PDO::PARAM_STR, 150);			
		$stmt->bindParam(8,	$intIseq          , PDO::PARAM_INT, 11);
		$stmt->bindParam(9, $intType          , PDO::PARAM_INT, 4);
		$stmt->bindParam(10, $strTag          , PDO::PARAM_STR, 100);		
        $stmt->execute();
		$arrResult 	= $stmt->fetch();		
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;
		if($intError == 0) {			
		}
		return $intError;
	}
	public function updateStatus($intLoginId, $intCategoryId, $intStatus, $intPerm) {
					
		$stmt = $this->database->prepare('call categories_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;
		if($intError == 0) {
		}
		return $intError;
	}
	public function delete($intLoginId, $intCategoryId) {
				
		$stmt = $this->database->prepare('call categories_delete(?,?)');
		$stmt->bindParam(1, $intLoginId, PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);
        $stmt->execute();
		$arrResult 	= $stmt->fetch();
		$intError	= isset($arrResult['errCode']) ? $arrResult['errCode'] : 0;
		if($intError == 0) {
		}
		return $intError;
	}
	public function deleteImg($strIcon) {	
		if(file_exists(DIR_CATEGORY . $strIcon))
			@unlink(DIR_CATEGORY . $strIcon);
	}

}